/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.composites;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory;
import org.eclipse.uml2.uml.Continuation;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;

/**
 * @author ymortier <yann.mortier@obeo.fr>
 */
public class IsSettingComposite implements ISectionComposite {

    /**
     * 
     */
    private Element element;

    /**
     * 
     */
    protected DirectEditSelectionListener directEditSelectionListener =
            new DirectEditSelectionListener();

    /**
     * @return the element
     */
    public Element getElement() {
        return element;
    }

    /**
     * @param element
     *            the element to set
     */
    public void setElement(Element element) {
        this.element = element;
    }

    /**
     * 
     */
    private Button isSetting = null;

    /*
     * (non-Javadoc)
     * 
     * @see com.cea.papyrus.core.ui.composites.ISectionComposite#createContent(org.eclipse.swt.widgets.Composite,
     *      org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetWidgetFactory)
     */
    /**
     * @param factory
     * @param parent
     * @return
     */
    public Composite createContent(Composite parent, TabbedPropertySheetWidgetFactory factory) {
        Composite composite = factory.createFlatFormComposite(parent);
        FormData data;

        isSetting = factory.createButton(composite, "", SWT.CHECK);
        data = new FormData();
        data.left = new FormAttachment(0, 85);
        data.top = new FormAttachment(0, 0);
        isSetting.setLayoutData(data);
        isSetting.addSelectionListener(directEditSelectionListener);

        CLabel abstractionLabel = factory.createCLabel(composite, "Setting:"); //$NON-NLS-1$
        data = new FormData();
        data.left = new FormAttachment(0, 0);
        data.right = new FormAttachment(isSetting, -ITabbedPropertyConstants.HSPACE);
        data.top = new FormAttachment(isSetting, 0, SWT.CENTER);
        abstractionLabel.setLayoutData(data);

        return composite;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.cea.papyrus.core.ui.composites.ISectionComposite#refresh()
     */
    /**
     * 
     */
    public void refresh() {
        if (!isSetting.isDisposed()) {
            isSetting.removeSelectionListener(directEditSelectionListener);

            if (element instanceof Continuation) {
                isSetting.setSelection(((Continuation) getElement()).isSetting());
            }
            isSetting.addSelectionListener(directEditSelectionListener);
        }
    }

    /**
     * Disposing listeners.
     */
    public void dispose() {
        if (isSetting != null && !isSetting.isDisposed())
            isSetting.removeSelectionListener(directEditSelectionListener);
    }

    /**
     * Listener for a direct edit manager.
     * 
     * @author Remi SCHNEKENBURGER
     */
    protected class DirectEditSelectionListener implements org.eclipse.swt.events.SelectionListener {

        /**
         * @param e
         */
        public void widgetSelected(SelectionEvent e) {
            if (element instanceof ElementImport) {// in the case of element import
                // do nothing
            } else if ((element instanceof Continuation)) {
                ((Continuation) element).setSetting(isSetting.getSelection());
            }
        }

        /**
         * @param e
         */
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    };

}
